"""
日记数据模型
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, JSON, Float
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship

from app.database import Base


class Diary(Base):
    """日记模型"""
    __tablename__ = "diaries"

    id = Column(Integer, primary_key=True, index=True, comment="日记ID")
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, comment="用户ID")
    title = Column(String(200), nullable=False, comment="标题")
    content = Column(Text, nullable=False, comment="内容(markdown)")
    tags = Column(JSON, default=list, comment="标签列表")
    mood = Column(Float, nullable=True, comment="情绪评分(-1到1)")
    word_count = Column(Integer, default=0, comment="字数统计")
    
    # 时间字段
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")
    
    # 关联关系
    user = relationship("User", back_populates="diaries")
    embeddings = relationship("Embedding", back_populates="diary", cascade="all, delete-orphan")

    def __repr__(self):
        return f"<Diary(id={self.id}, title='{self.title}', user_id={self.user_id})>" 